<template>
<div class="selector-input">
    <!-- <label class="placeholder">{{ placeholder }}</label> -->
    <input type="text" :placeholder="placeholder" class="input" :value="value" @input="searchOptions($event)" @focus="searchOptions($event)" @blur="setValue(value)" ref="inputValue" />
    <div class="iconfont">
        <span class="icon-search"></span>
    </div>
</div>
</template>

<script>
import {
    getCurrentInstance
} from 'vue'
export default {
    name: 'SelectorInput',
    props: {
        placeholder: {
            type: String,
            default: '请选择',
        },
        value: String
    },
    setup(props, ctx) {
        const instance = getCurrentInstance();

        const setValue = (value) => {
            const _input = instance.refs.inputValue

            if (_input.value.length > 0) {
                _input.value = value
            }
        }

        const searchOptions = (e) => {
            ctx.emit("searchOptions", e.target.value);
        }
        return {
            searchOptions,
            setValue
        }
    }
}
</script>

<style lang="scss" scoped>
.selector-input {
    position: relative;
    width: 100%;
    height: 38px;

    .input {
        width: 100%;
        height: 100%;
        padding: 0 15px;
        box-sizing: border-box;
        background-color: #f2f2f2;
        border: 1px solid transparent;
        border-radius: 40px;
        outline: none;
        transition: all 0.2s linear;

        &:focus {
            border-color: $ThemeColor;
            box-shadow: 0 0 3px $ThemeColor;
        }
    }

    .placeholder,
    .iconfont {
        position: absolute;
    }

    .placeholder {
        left: 15px;
        top: 50%;
        margin-top: -8px;
        height: 19px;
        color: #999;
        overflow: hidden;
    }

    .iconfont {
        width: 36px;
        height: 36px;
        right: 2px;
        top: 1px;
        border-radius: 40px;
        text-align: center;
        background-color: #f2f2f2;
        line-height: 37px;
    }

    .icon-search {
        width: 12px;
        height: 12px;
        display: inline-block;
        border: 2px solid #999;
        border-radius: 50%;
        position: relative;
        cursor: pointer;

        &::before {
            content: "";
            width: 8px;
            height: 2px;
            border-width: 0;
            background-color: #999;
            position: absolute;
            left: 9px;
            bottom: -4px;
            transform: rotate(45deg);
        }

        &:hover {
            border: 2px solid $ThemeColor;

            &::before {
                background-color: $ThemeColor;
            }
        }
    }
}
</style>
